local Test = {}

function Test:_MakeTT()
	if self.tt then return end
	local tt = CreateFrame"GameTooltip"
	local lefts, rights = {}, {}
	for i = 1, 20 do
		local left, right = tt:CreateFontString(), tt:CreateFontString()
		left:SetFontObject(GameFontNormal)
		right:SetFontObject(GameFontNormal)
		tt:AddFontStrings(left, right)
		lefts[i], rights[i] = left, right
	end
	tt.lefts, tt.rights = lefts, rights

	function tt:SetSpell(spell)
		self:SetOwner(UIParent, "ANCHOR_CURSOR")
		self:ClearLines()
		self:SetHyperlink("spell:"..spell)
	end
	self.tt = tt
end

function Test:_FillSpell(spell, ranks)
	self.tt:SetSpell(spell)
	local lines = self.tt:NumLines()
	while lines >= 1 do
		local l = self.tt.rights[lines]
		if not l or not l:IsVisible() or not l:GetText() or l:GetText() == '' then
			lines = lines - 1
		else
			break
		end
	end
	local info = self.lineinfo
	if not info then
		info = {}
		self.lineinfo = info
		self.maxline = 0
	end
	local rinfo = info[ranks]
	if not rinfo then
		rinfo = {}
		info[ranks] = rinfo
	end
	rinfo[lines] = (rinfo[lines] or 0) + 1
	if lines > self.maxline then
		self.maxline = lines
		self.maxline_spell = spell
	end
end

function Test:_Report()
	for rank, rinfo in pairs(self.lineinfo) do
		for lines, count in pairs(rinfo) do
			print(rank, lines, count)
		end
	end
end

function Test:Fill()
	self:_MakeTT()
	for class in pairs(RAID_CLASS_COLORS) do
		for _, tab in ipairs(Talented:UncompressSpellData(class)) do
			for _, ranks in ipairs(tab) do
				self:_FillSpell(ranks[1], #ranks)
			end
		end
	end
	self:_Report()
end

function Test:_GetTTPos(text)
	for i = 1, 20 do
		local left, right = self.tt.lefts[i], self.tt.rights[i]
		left, right = left:GetText(), right:GetText()
		if left == text then
			return "line "..i.." -left"
		elseif right == text then
			return "line "..i.." -right"
		end
	end
	return "*not found*"
end

function Test:_CheckTTLine(index, region)
	local name = region:GetName()
	local text = region:GetText()
	if text:len() > 20 then
		text = text:sub(1, 20).." ..."
	end
	local anchors = region:GetNumPoints()
	local position = self:_GetTTPos(text)
	print(index, position, name, text, anchors, region:GetPoint(1))
end

function Test:_CheckTTLines(...)
	print("number of regions", select("#", ...))
	for i = 1, select("#", ...) do
		local region = select(i, ...)
		if region and region.IsObjectType and region:IsObjectType("FontString") and region:IsVisible() then
			self:_CheckTTLine(i, region)
		end
	end
end

function Test:CheckTooltip()
	local ott, tt = self.ott, self.tt
	if not ott then
		ott = CreateFrame("GameTooltip", "TTT_Tooltip", UIParent)
		self.ott = ott
		ott.SetSpell = tt.SetSpell
	end
	tt:SetSpell(self.maxline_spell)
	tt:Show()
	ott:SetSpell(self.maxline_spell)
	ott:Show()
	self:_CheckTTLines(ott:GetRegions())
end

function Test:item()
	local ott = self.ott
	ott:SetOwner(UIParent, "ANCHOR_CURSOR")
	ott:ClearLines()
	ott:SetHyperlink("item:25628")
	ott:Show()
end

SlashCmdList.JERRY_TTT = function (input)
	LoadAddOn"Talented"
	Test:Fill()
	Test:CheckTooltip()
end
SLASH_JERRY_TTT1="/ttt"
_G.TalentTooltipTest = Test
